﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace QuickApp
{
    public static class DataGridViewHelper
    {
        /// <summary>
        /// Returns an array of datarow objects based on what is selected on the grid.
        /// </summary>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public static DataRow[] GetSelectedRows(this DataGridView dgv)
        {
            Type srcType= dgv.DataSource.GetType();
            if (srcType != typeof(DataTable) && srcType != typeof(DataView))
                return null;
            DataRow[] selectedRows;
            if (dgv.SelectedRows.Count > 0)
            {
                selectedRows = new DataRow[dgv.SelectedRows.Count];
                for (int i = 0; i < selectedRows.Length; ++i)
                {
                    selectedRows[i] = ((DataRowView)dgv.SelectedRows[i].DataBoundItem).Row;
                }
                return selectedRows;
            }
            if (dgv.CurrentRow == null)
                return null;
            selectedRows = new DataRow[1];
            selectedRows[0] = ((DataRowView)dgv.CurrentRow.DataBoundItem).Row;
            return selectedRows;
        }
        /// <summary>
        /// Adds a last column to a datagridview to cancel the empty space.
        /// </summary>
        /// <param name="dgv"></param>
        public static void AddLastColumnToDataGridView(this DataGridView dgv)
        {
            if (!dgv.Columns.Contains("blankColumn"))
            {
                DataGridViewColumn dgvc = dgv.Columns[dgv.Columns.Add("blankColumn", "")];
                dgvc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgvc.Resizable = DataGridViewTriState.False;
                dgvc.ReadOnly = true;
            }
        }
        public static void ResizeToFields(this DataGridView grid)
        {
            foreach (DataGridViewColumn col in grid.Columns)
            {
                if (col.Name != "blankColumn")
                {
                    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                }
            }
            grid.Refresh();
            foreach (DataGridViewColumn col in grid.Columns)
            {
                if (col.Name != "blankColumn")
                {
                    col.Width = col.Width;
                    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                }
            }
        }
        private static void SetGridBackColorAlternatingRowsStyle(this DataGridView grd)
        {
            Color c = grd.RowCount % 2 == 1 ? grd.AlternatingRowsDefaultCellStyle.BackColor : grd.DefaultCellStyle.BackColor;
            if (grd.DataSource != null && !c.IsEmpty)
            {
                grd.BackgroundColor = c;
            }
        }
    }
}


